package fun.stgoder.dev.listener;

import cn.hutool.core.util.StrUtil;
import fun.stgoder.dev.listener.common.Constants;
import fun.stgoder.dev.listener.sdk.FaceMsgTask;
import fun.stgoder.dev.listener.sdk.HCNetSDKCache;
import fun.stgoder.dev.listener.sdk.dh.DhSDKCache;
import fun.stgoder.dev.listener.udp.UdpServer;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;

public class App {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(FaceMsgTask.class);

    public static void main(String[] args) throws Exception {

        // for test
        args = new String[]{"10105", "admin:a00000000@192.168.1.108:37777"};
        System.setProperty("logLevel", "debug");

        String logLevel = System.getProperty("logLevel");

        Logger rootLogger = Logger.getRootLogger();
        if (StrUtil.isNotBlank(logLevel)) {
            if (StrUtil.equals(logLevel, Level.ALL.toString(), true)) {
                rootLogger.setLevel(Level.ALL);
            } else if (StrUtil.equals(logLevel, Level.TRACE.toString(), true)) {
                rootLogger.setLevel(Level.TRACE);
            } else if (StrUtil.equals(logLevel, Level.DEBUG.toString(), true)) {
                rootLogger.setLevel(Level.DEBUG);
            } else if (StrUtil.equals(logLevel, Level.INFO.toString(), true)) {
                rootLogger.setLevel(Level.INFO);
            } else if (StrUtil.equals(logLevel, Level.WARN.toString(), true)) {
                rootLogger.setLevel(Level.WARN);
            } else if (StrUtil.equals(logLevel, Level.INFO.toString(), true)) {
                rootLogger.setLevel(Level.INFO);
            } else if (StrUtil.equals(logLevel, Level.ERROR.toString(), true)) {
                rootLogger.setLevel(Level.ERROR);
            } else if (StrUtil.equals(logLevel, Level.FATAL.toString(), true)) {
                rootLogger.setLevel(Level.FATAL);
            } else if (StrUtil.equals(logLevel, Level.OFF.toString(), true)) {
                rootLogger.setLevel(Level.OFF);
            }
        }
        FileAppender appender = (FileAppender) rootLogger.getAppender("FILE");

        // args
        String udpPortStr = args[0];
        int udpPort = Integer.parseInt(udpPortStr);
        Constants.SELF_UDP_PORT = udpPort;

        String logfile = appender.getFile();
        logfile += udpPortStr;
        appender.setFile(logfile);

        appender.activateOptions();

        logger.info("udp port: " + udpPort);
        logger.info("log file - " + logfile);

        UdpServer.createInstance();

        for (int i = 1; i < args.length; i++) {
            // username:password@ip:port
            // eg: [admin:a00000000@192.168.1.71:8000]
            try {
                String arg = args[i];
                String[] split = arg.split("@");
                String s1 = split[0];
                String[] split1 = s1.split(":");
                String username = split1[0];
                String password = split1[1];
                String s2 = split[1];
                String[] split2 = s2.split(":");
                String ip = split2[0];
                String portStr = split2[1];
                int port = Integer.parseInt(portStr);

                if (Constants.SDK_USE_DAHUA) {
                    DhSDKCache.initAndPut(ip, port, username, password);
                } else {
                    HCNetSDKCache.initAndPut(ip, port, username, password);
                }
            } catch (Exception e) {
                logger.error("init sdk loop", e);
            }
        }

        /*HCNetSDKWrapper sdk = new HCNetSDKWrapper("192.168.1.71", 8000, "admin", "a00000000");
        sdk.init();
        sdk.login_V40();*/

    }
}
